package cn.iocoder.yudao.module.picturebook.controller.admin.plan;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.picturebook.controller.admin.plan.vo.*;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.plan.PlanDO;
import cn.iocoder.yudao.module.picturebook.service.plan.PlanService;

@Tag(name = "绘本系统 - 每日任务表	")
@RestController
@RequestMapping("/picturebook/plan")
@Validated
public class PlanController {

    @Resource
    private PlanService planService;

    @PostMapping("/create")
    @Operation(summary = "创建每日任务表	")
    @PreAuthorize("@ss.hasPermission('picturebook:plan:create')")
    public CommonResult<Integer> createPlan(@Valid @RequestBody PlanSaveReqVO createReqVO) {
        return success(planService.createPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新每日任务表	")
    @PreAuthorize("@ss.hasPermission('picturebook:plan:update')")
    public CommonResult<Boolean> updatePlan(@Valid @RequestBody PlanSaveReqVO updateReqVO) {
        planService.updatePlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除每日任务表	")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('picturebook:plan:delete')")
    public CommonResult<Boolean> deletePlan(@RequestParam("id") Integer id) {
        planService.deletePlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除每日任务表	")
                @PreAuthorize("@ss.hasPermission('picturebook:plan:delete')")
    public CommonResult<Boolean> deletePlanList(@RequestParam("ids") List<Integer> ids) {
        planService.deletePlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得每日任务表	")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('picturebook:plan:query')")
    public CommonResult<PlanRespVO> getPlan(@RequestParam("id") Integer id) {
        PlanDO plan = planService.getPlan(id);
        return success(BeanUtils.toBean(plan, PlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得每日任务表	分页")
    @PreAuthorize("@ss.hasPermission('picturebook:plan:query')")
    public CommonResult<PageResult<PlanRespVO>> getPlanPage(@Valid PlanPageReqVO pageReqVO) {
        PageResult<PlanDO> pageResult = planService.getPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出每日任务表	 Excel")
    @PreAuthorize("@ss.hasPermission('picturebook:plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlanExcel(@Valid PlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlanDO> list = planService.getPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "每日任务表	.xls", "数据", PlanRespVO.class,
                        BeanUtils.toBean(list, PlanRespVO.class));
    }

}